|
|
|
|
|
|
|
The last routine in your library performs an update of an existing record. The simplest way to accomplish this is to delete the existing record and replace it with the new updated version. This can be done with two SQL statements: a DELETE query, followed by an INSERT INTO statement. A more sophisticated approach would be to build a series of UPDATE statements that update each field of the row, one at a time. For the example here, you use the DELETE/INSERT method, because it takes less code and is easier to understand. |
|
|
|
|
| |
|
|
|
|
In certain situations, you do not want to perform updates by using the DELETE/INSERT method. If you have defined a delete cascade in a relationship between two tables, performing a DELETE/INSERT on the one side of the one-to-many relationship results in the deletion of all the related records on the many side of the relationship. In cases where yot?might define delete cascades, you should use only the UDPATE method. |
|
|
|
|
|
|
|
|
|
|
Create the UpdateRow method and add the code in Listing 19.22. |
|
|
|
|
|
|
|
|
Listing 19.22. Coding the UpdateRow method. |
|
|
|
|
|
|
|
|
Public Sub UpdateRow(frmTemp As Object)
'
' replace an existing row
'
Dim intResult As Integer
Dim strDelSQL As String
Dim strAddSQL As String
Dim intLoop As Integer
'
strDelSQL = "DELETE * FROM " & strTable & " WHERE "
strDelSQL = strDelSQL & strKey & "='" & frmTemp.sqlfield(0) & "'"
'
strAddSQL = "INSERT INTO " & strTable & " VALUES("
'
For intLoop = 1 To NumCols
strAddSQL = strAddSQL & "'" & frmTemp.sqlfield(intLoop - 1) & "'"
If intLoop <> NumCols Then
strAddSQL = strAddSQL & ","
End If
Next
strAddSQL = strAddSQL & ")"
'
' make the calls
intResult = SQLExecDirect(hStmt, strDelSQL, Len(strDelSQL))
If intResult <> sqlSuccess Then
sqlErrorMsg "Unable to delete row"
intResult = SQLFreeStmt(hStmt, sqlClose)
End If |
|
|
|
|
|